// Copyright (c) Appeon. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. 
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.

using System;
using System.Collections.Generic;
using System.Linq.Expressions;

namespace SnapObjects.Expressions
{
    public class CompileParserOptions : ParserOptions<Expression>
    {
        protected readonly List<ParameterExpression> _useds;

        public CompileParserOptions(
            string expression,
            string sourceKind,
            Type declaredType,
            object dataSource = null,
            object customData = null)
            : base(expression,
                  sourceKind,
                  dataSource,
                  new CompileMetadataCollection(),
                  customData)
        {
            _useds = new List<ParameterExpression>();

            this.DeclaredType = declaredType;
        }

        public Type DeclaredType { get; }
        public IReadOnlyList<ParameterExpression> UsedParameters => _useds;
        internal ParameterExpression SubModelParameter { get; private set; }

        public void AddUsedParameter(
            ParameterExpression parameter,
            bool isModelParameter = false)
        {
            if (!_useds.Contains(parameter))
            {
                _useds.Add(parameter);
            }

            if (isModelParameter)
            {
                this.SubModelParameter = parameter;
            }
        }

        protected void SetSubModelParameter(
            ParameterExpression parameter = null)
        {
            this.SubModelParameter = parameter;
        }
    }
}
